package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.Course;
import com.campsys.entity.RollCall;
import com.campsys.entity.User;
import com.campsys.service.CourseService;
import com.campsys.service.dto.CourseDTO;
import com.campsys.service.dto.CourseQueryDTO;
import com.campsys.service.dto.RollCallDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teacher/course")
@Slf4j
public class TeacherCourseController {
    
    @Autowired
    private CourseService courseService;
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:view")
    @PostMapping("/list")
    public Result getCourseList(@RequestBody CourseQueryDTO queryDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        Page<Course> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(courseService.getCourseList(page, queryDTO, teacher.getId()));
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:add")
    @PostMapping("/add")
    public Result addCourse(@RequestBody @Validated CourseDTO courseDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        courseService.addCourse(courseDTO, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:edit")
    @PostMapping("/update")
    public Result updateCourse(@RequestBody @Validated CourseDTO courseDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        courseService.updateCourse(courseDTO, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:delete")
    @PostMapping("/delete/{id}")
    public Result deleteCourse(@PathVariable Long id) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        courseService.deleteCourse(id, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:edit")
    @PostMapping("/status/{id}")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        courseService.updateStatus(id, status, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:student")
    @PostMapping("/{id}/students")
    public Result getCourseStudents(@PathVariable Long id, 
                                  @RequestParam Integer pageNum, 
                                  @RequestParam Integer pageSize) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        Page<User> page = new Page<>(pageNum, pageSize);
        return Result.ok(courseService.getCourseStudents(page, id, teacher.getId()));
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:roll-call")
    @PostMapping("/roll-call/create")
    public Result createRollCall(@RequestBody @Validated RollCallDTO rollCallDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        courseService.createRollCall(rollCallDTO, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("course:roll-call")
    @PostMapping("/{id}/roll-calls")
    public Result getRollCallList(@PathVariable Long id,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        Page<RollCall> page = new Page<>(pageNum, pageSize);
        return Result.ok(courseService.getRollCallList(page, id, teacher.getId()));
    }
} 